package com.aloda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.aloda.constant.ExceptionConstant;
import com.aloda.entity.Emp;
import com.aloda.entity.OperateLog;
import com.aloda.mapper.OperateLogMapper;
import com.aloda.query.OpLogPageDTO;
import com.aloda.result.PageResult;
import com.aloda.service.OperateLogService;
import com.aloda.vo.EmpExportVO;
import com.aloda.vo.OpLogExportVO;
import com.aloda.vo.OpLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author aloda
 * @description 针对表【operate_log(操作日志表)】的数据库操作Service实现
 * @createDate 2024-12-18 17:18:09
 */
@Service
public class OperateLogServiceImpl extends ServiceImpl<OperateLogMapper, OperateLog> implements OperateLogService {

    @Resource
    private OperateLogMapper operateLogMapper;

    /**
     * 分页查询
     *
     * @param opLogPageDTO
     * @return
     */
    @Override
    public PageResult<OpLogVO> pageQuery(OpLogPageDTO opLogPageDTO) {
        Page<OperateLog> page = opLogPageDTO.toMpPage("op_time", false);
        LambdaQueryWrapper<OperateLog> operateLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        operateLogLambdaQueryWrapper
                .like(opLogPageDTO.getEmpName() != null, OperateLog::getEmpName, opLogPageDTO.getEmpName())
                .like(opLogPageDTO.getCategory() != null, OperateLog::getCategory, opLogPageDTO.getCategory())
                .between(opLogPageDTO.getEndTime() != null && opLogPageDTO.getBeginTime() != null,
                        OperateLog::getOpTime, opLogPageDTO.getBeginTime(), opLogPageDTO.getEndTime());
        operateLogMapper.selectPage(page,operateLogLambdaQueryWrapper);
        return PageResult.of(page, OpLogVO.class);
    }

    /**
     * 导出操作日志信息到excel
     * @param response
     * @param ids
     */
    @Override
    public void exportOpLogToExcel(HttpServletResponse response, List<Long> ids) {
        try {
            // 设置响应头
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("操作日志信息表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

            // 查询所有信息 如果ids为空查询所有信息，否则查询对应信息
            LambdaQueryWrapper<OperateLog> queryWrapper = new LambdaQueryWrapper<>();
            List<OperateLog> operateLogList;
            if (ids.isEmpty()) {
                operateLogList = operateLogMapper.selectList(queryWrapper);
            } else {
                operateLogList = operateLogMapper.selectBatchIds(ids);
            }

            // 转换为导出VO
            List<OpLogExportVO> exportVOList = operateLogList.stream()
                    .map(operateLog -> BeanUtil.copyProperties(operateLog, OpLogExportVO.class)).toList();

            // 使用EasyExcel导出
            EasyExcel.write(response.getOutputStream(), OpLogExportVO.class)
                    .sheet("操作日志信息")
                    .doWrite(exportVOList);

        } catch (IOException e) {
            throw new RuntimeException(ExceptionConstant.EXPORT_EXCEL_ERROR, e);
        }
    }
}




